package com.xiaoq.sites.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.coms.util.IdGeneratorUtils;
import com.xiaoq.sites.entity.InfrastructureBaseEntity;
import com.xiaoq.sites.repository.IInfrastructureRepository;


@Repository
public class InfrastructureRepositoryImpl implements IInfrastructureRepository {

	@Autowired
	private Datastore datastore;

	@Override
	public List<InfrastructureBaseEntity> findInfraBySitesIdAndInfraType(String sitesId, Integer infraType) {
		if (StringUtils.isNotBlank(sitesId)) {
			Query<InfrastructureBaseEntity> infrastructureBaseEntityQuery = datastore
					.find(InfrastructureBaseEntity.class);
			if (infraType!=null && infraType != 0) {
				infrastructureBaseEntityQuery.field("infraType").equal(infraType);
			}

			infrastructureBaseEntityQuery.field("sitesId").equal(sitesId);
			return infrastructureBaseEntityQuery.asList();
		}
		return null;

	}

	@Override
	public boolean addSitesInfraType(InfrastructureBaseEntity infrastructureBase) {
		datastore.save(infrastructureBase);
		return true;
	}

	@Override
	public boolean deleteSitesInfraType(String infrastructId) {
		Query<InfrastructureBaseEntity> infrastructQuery = datastore.find(InfrastructureBaseEntity.class).field(IdGeneratorUtils.IDGENERATOR).equal(infrastructId);
		datastore.delete(infrastructQuery);
		return true;
	}

	@Override
	public InfrastructureBaseEntity findInfraById(String id) {
		InfrastructureBaseEntity infrastruct = datastore.find(InfrastructureBaseEntity.class).field(IdGeneratorUtils.IDGENERATOR).equal(id).get();
		return infrastruct;
	}

	@Override
	public List findSitesInfraTypeByCityCode(String cityCode, Integer infraType) {
		Query<InfrastructureBaseEntity> infrastructureBaseEntityQuery = datastore
				.find(InfrastructureBaseEntity.class);

		infrastructureBaseEntityQuery.field("infraType").equal(infraType);
		infrastructureBaseEntityQuery.field("cityCode").equal(cityCode);
		return infrastructureBaseEntityQuery.asList();
	}


}
